package nccloud.web.ic.custrecloss.action.transfer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nc.ui.querytemplate.querytree.IQueryScheme;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.m4y.entity.TransOutVO;
import nc.vo.ic.pub.util.StringUtil;
import nc.vo.ic.pub.util.ValueCheckUtil;
import nc.vo.pub.BusinessException;
import nc.vo.scmpub.res.billtype.ICBillType;
import nc.vo.scmpub.util.BillBodySortUtils;
import nccloud.base.exception.ExceptionUtils;
import nccloud.dto.baseapp.querytree.dataformat.Condition;
import nccloud.dto.baseapp.querytree.dataformat.CustCondition;
import nccloud.dto.baseapp.querytree.dataformat.QueryTreeFormatVO;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.framework.web.ui.pattern.billgrid.BillGrid;
import nccloud.framework.web.ui.pattern.billgrid.BillGridOperator;
import nccloud.nc.vo.pub.pf.PullBillInfoVO;
import nccloud.pubitf.ic.custrecloss.service.IWastAgeFetchBillService;
import nccloud.pubitf.platform.query.INCCloudQueryService;
import nccloud.pubitf.riart.businesstype.IPFBusinessTypeService;



public class WastAgeQryAllAction
  implements ICommonAction
{
  public Object doAction(IRequest request) {
    String read = request.read();
    IJson json = JsonFactory.create();
    
    QueryTreeFormatVO info = (QueryTreeFormatVO)json.fromJson(read, QueryTreeFormatVO.class);
    QueryTreeFormatVO saleInfo = (QueryTreeFormatVO)json.fromJson(read, QueryTreeFormatVO.class);
    QueryTreeFormatVO transInfo = (QueryTreeFormatVO)json.fromJson(read, QueryTreeFormatVO.class);
    saleInfo.setCustcondition(null);
    saleInfo.setCustcondition(null);

    
    INCCloudQueryService queryutil = (INCCloudQueryService)ServiceLocator.find(INCCloudQueryService.class);
    IQueryScheme scheme = queryutil.convertCondition(saleInfo);
    Object transtype = null;
    PullBillInfoVO[] refbillinfos = queryRefPara(transtype);
    appendRefWhr(scheme, refbillinfos, transtype);


    
    List<BillGrid> grids = null;
    Condition templateids = null, oids = null, pagecodes = null;
    CustCondition custcondition = info.getCustcondition();
    for (Condition condition : custcondition.getConditions()) {
      if (condition.getField().equals("templates")) {
        templateids = condition; continue;
      }  if (condition.getField().equals("oids")) {
        oids = condition; continue;
      }  if (condition.getField().equals("pagecode")) {
        pagecodes = condition;
      }
    } 
    
    try {
      saleInfo.setOid(oids.getValue().getFirstvalue());
      transInfo.setOid(oids.getValue().getSecondvalue());
      IWastAgeFetchBillService service = (IWastAgeFetchBillService)ServiceLocator.find(IWastAgeFetchBillService.class);
      SaleOutVO[] saleVOs = service.querySaleOutBill(scheme);
      TransOutVO[] transVOs = service.queryTransOutBill(transInfo);
      
      if (refbillinfos == null || refbillinfos.length < 1) {
        saleVOs = null;
      }
      grids = new ArrayList<BillGrid>();
      if (saleVOs != null && saleVOs.length != 0) {
        BillBodySortUtils.sort(saleVOs);
        
        BillGrid[] saleGrids = (new BillGridOperator(templateids.getValue().getFirstvalue(), pagecodes.getValue().getFirstvalue())).toBillGrids(saleVOs);
        (new WastAgeQrySaleOutAction()).processResult(saleGrids, saleVOs);
        grids.addAll(Arrays.asList(saleGrids));
      } 
      if (transVOs != null && transVOs.length != 0) {
        BillBodySortUtils.sort(transVOs);
        
        BillGrid[] transGrids = (new BillGridOperator(templateids.getValue().getSecondvalue(), pagecodes.getValue().getSecondvalue())).toBillGrids(transVOs);
        (new WastAgeQryTransOutAction()).processResult(transGrids, transVOs);
        grids.addAll(Arrays.asList(transGrids));
      } 
    } catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    return grids;
  }
  
  private PullBillInfoVO[] queryRefPara(Object transtype) {
    ClientInfo clientinfo = SessionContext.getInstance().getClientInfo();
    String groupId = clientinfo.getPk_group();
    String userId = clientinfo.getUserid();
    IPFBusinessTypeService pfservice = (IPFBusinessTypeService)ServiceLocator.find(IPFBusinessTypeService.class);
    PullBillInfoVO[] refbillinfos = null;
    try {
      refbillinfos = pfservice.getPullBillInfo(ICBillType.WastageBill.getCode(), (null == transtype) ? null : (String)transtype, groupId, userId);
    }
    catch (BusinessException e) {
      ExceptionUtils.wrapException(e);
    } 
    
    return refbillinfos;
  }
  
  private void appendRefWhr(IQueryScheme scheme, PullBillInfoVO[] refbillinfos, Object obj) {
    if (null == refbillinfos || refbillinfos.length == 0) {
      return;
    }
    List<String> busitypes = new ArrayList<String>();
    List<String> transtypes = new ArrayList<String>();
    for (PullBillInfoVO info : refbillinfos) {
      if ("4C".equals(info.getSrc_billtype())) {
        List<String> busitypes1 = info.getBusitypes();
        List<String> transtypes1 = info.getTranstypes();
        busitypes.addAll(busitypes1);
        if (!transtypes1.contains("")) {
          transtypes1.addAll(transtypes);
        }
      } 
    } 
    
    if (!ValueCheckUtil.isNullORZeroLength(busitypes) && !StringUtil.isSEmptyOrNull((String)busitypes.get(0))) {
      scheme.put("busitypearray", busitypes.toArray(new String[0]));
      if (!ValueCheckUtil.isNullORZeroLength(transtypes) && !StringUtil.isSEmptyOrNull((String)transtypes.get(0)))
        scheme.put("transtypearray", transtypes.toArray(new String[0])); 
    } 
  }
}